package com.yonyou.cyxdms.basedata.client.service.basedata;

import com.yonyou.cyx.framework.bean.dto.base.ClientInfoDTO;
import com.yonyou.cyx.framework.bean.dto.framework.RestResultResponse;
import com.yonyou.cyx.framework.compent.redis.RedisClient;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyx.function.utils.jsonserializer.JSONUtil;
import com.yonyou.cyxdms.basedata.client.constants.BaseDataCommonConstants;
import com.yonyou.cyxdms.basedata.client.domains.vo.VehicleInfoVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.VsMasterInfoVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.VsOptionVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.VsProductVO;
import com.yonyou.cyxdms.basedata.client.dto.CmsApp04;
import com.yonyou.cyxdms.basedata.client.dto.CmsApp04QueryDto;
import com.yonyou.cyxdms.basedata.client.dto.VsProductDlrVO;
import com.yonyou.cyxdms.basedata.client.dto.option.VsOptionDTO;
import com.yonyou.cyxdms.basedata.client.dto.product.VehicleInfoDTO;
import com.yonyou.cyxdms.basedata.client.dto.wholesales.PlanTemplateDTO;
import com.yonyou.cyxdms.basedata.client.feign.BaseDataCommonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

@Service
public class BaseCommonVsProductServiceImpl implements BaseCommonVsProductService {

    private final Logger logger = LoggerFactory.getLogger(BaseCommonVsProductServiceImpl.class);

    @Autowired
    private BaseDataCommonClient baseDataCommonClient;

    @Autowired
    private RedisClient redisClient;

    /**
     * 根据id查询整车产品(tm_vs_product、tm_vs_product_price)
     *
     * @param productId productId
     * @return com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductIdVO
     * @author dengqiang
     * @since 2019/8/22
     */
    @Override
    public VsMasterInfoVO queryProductInfoById(Long productId) {
        ClientInfoDTO loginInfoDto = ApplicationContextHelper.getBeanByType(ClientInfoDTO.class);
        String messageFormat = MessageFormat.format(BaseDataCommonConstants.VSPRODUCT_CACHE_VSPRODUCT, loginInfoDto.getAppId(), loginInfoDto.getOwnerCode());
        Object vsProductDto = redisClient.hget(messageFormat, String.valueOf(productId));
        if (null == vsProductDto) {
            // 查询所有数据
            loadValueIfNotExists(productId, messageFormat);
            Object redisvsProductDto = redisClient.hget(messageFormat, String.valueOf(productId));
            if (null == redisvsProductDto) {
                return null;
            }
            return JSONUtil.jsonToObj(JSONUtil.objectToJson(redisvsProductDto), VsMasterInfoVO.class);
        }
        else {
            return JSONUtil.jsonToObj(JSONUtil.objectToJson(vsProductDto), VsMasterInfoVO.class);
        }
    }

    /**
     * 不登陆获取整车产品信息(tm_vs_product)
     *
     * @param productId productId
     * @return com.yonyou.cyxdms.basedata.client.domains.vo.VsMasterInfoVO
     * @author renwd
     * @since 2019/12/7
     */
    @Override
    public VsMasterInfoVO getProductInfoById(Long productId) {
        Object vsProductDto = redisClient.hget(BaseDataCommonConstants.VSPRODUCT_CACHE, String.valueOf(productId));
        if (null == vsProductDto) {
            // 查询所有数据
            VsMasterInfoVO vsMasterInfoVO = baseDataCommonClient.queryTccProductInfoById(productId).getData();
            redisClient.hsetExpire(BaseDataCommonConstants.VSPRODUCT_CACHE, String.valueOf(vsMasterInfoVO.getProductId()), JSONUtil.objectToJson(vsMasterInfoVO), BaseDataCommonConstants.EXPIRE_TIME_ONE_DAY);
            Object redisVsProduct = redisClient.hget(BaseDataCommonConstants.VSPRODUCT_CACHE, String.valueOf(productId));
            if (null == redisVsProduct) {
                return null;
            }
            return JSONUtil.jsonToObj(JSONUtil.objectToJson(redisVsProduct), VsMasterInfoVO.class);
        }
        else {
            return JSONUtil.jsonToObj(JSONUtil.objectToJson(vsProductDto), VsMasterInfoVO.class);
        }
    }

    /**
     * Tcc获取车辆主数据
     *
     * @auhtor ysj
     * @date 2019/11/19
     * @param productId
     * @return
     */
    @Override
    public VsMasterInfoVO queryTccProductInfoById(Long productId) {
        return baseDataCommonClient.queryTccProductInfoById(productId).getData();
    }

    /**
     * 根据产品名称查询产品配置ID
     *
     * @author ysj
     * @date 2019/10/21
     * @param vehicleInfoDTO
     * @return
     */
    @Override
    public VehicleInfoVO getVehicleInfoByName(VehicleInfoDTO vehicleInfoDTO) {
        return baseDataCommonClient.getVehicleInfoByName(vehicleInfoDTO).getData();
    }

    /**
     * 通过产品代码查出productIds
     *
     * @param productCode
     * @return java.util.List<java.lang.Long>
     * @author dengqiang
     * @since 2019/10/22
     */
    @Override
    public List<Long> queryProductIdsBy(String productCode) {
        return baseDataCommonClient.queryProductIdsBy(productCode).getData();
    }

    /**
     * 查询数据
     *
     * @param
     * @return void
     * @author dengqiang
     * @since 2019/8/22
     */
    private void loadValueIfNotExists(Long productId, String messageFormat) {
        VsMasterInfoVO vsProductResponse = baseDataCommonClient.queryProductInfoById(productId).getData();
        logger.info("feign查询vsProduct信息:{}", vsProductResponse);

        if (!StringUtils.isNullOrEmpty(vsProductResponse)) {
            initOneValue(vsProductResponse, messageFormat);
        }
    }

    /**
     * 将数据放置redis
     *
     * @param vsProduct
     * @return void
     * @author dengqiang
     * @since 2019/8/22
     */
    private void initOneValue(VsMasterInfoVO vsProduct, String key) {
        redisClient.hsetExpire(key, String.valueOf(vsProduct.getProductId()), JSONUtil.objectToJson(vsProduct), BaseDataCommonConstants.EXPIRE_TIME_ONE_DAY);
    }

    /**
     * 获取产品信息
     * @param vsOptionDTO
     * @return
     */
    @Override
    public List<VsProductVO> getproductInfo(VsOptionDTO vsOptionDTO){
        return baseDataCommonClient.getproductInfo(vsOptionDTO).getData();
    }

    /**
     * 获取产品信息[无分页]
     * @param vsOptionDTO
     * @return
     */
    @Override
    public List<VsProductVO> getInfoList(VsOptionDTO vsOptionDTO){
        return baseDataCommonClient.getInfoList(vsOptionDTO).getData();
    }

    /**
     * 获取需求计划模板信息列表
     *
     * @param productCodes 产品代码
     * @author lyz
     * @date 2020/11/10
     */
    @Override
    public List<PlanTemplateDTO> getPlanTemplates(List<String> productCodes) {
        logger.info("feign查询PlanTemplateDTO信息查询条件productCodes:{}", productCodes);
        return baseDataCommonClient.getPlanTemplates(productCodes).getData();
    }

    @Override
    public CmsApp04 searchCmsApp04Data(CmsApp04QueryDto cmsApp04QueryDto) {

        CmsApp04  cmsApp04= new  CmsApp04();
        RestResultResponse<CmsApp04> res= baseDataCommonClient.searchCmsApp04Data(cmsApp04QueryDto);

        if(res!=null){
            cmsApp04=res.getData();
        }
        return cmsApp04;
    }

    /**
     * 获取产品信息
     *
     * @param productCode
     * @return java.util.List<com.yonyou.cyxdms.basedata.client.domains.vo.VsProductVO>
     * @author caozx
     * @since 2021/01/28
     */
    @Override
    public VsProductVO getProduct(String productCode){
        return baseDataCommonClient.getProduct(productCode).getData();
    }

    /**
     * 获取产品信息
     *
     * @param productCode
     * @return com.yonyou.cyxdms.basedata.client.dto.VsProductDlrVO
     * @author caozx
     * @since 2021/02/22
     */
    @Override
    public VsProductDlrVO getProductNew(String productCode){
        return baseDataCommonClient.getProductNew(productCode).getData();
    }

}
